package org.scut.smartgeometry.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 13-6-28.
 */
public class FileUtils {

    //图片存储路径
    public static final String IMAGE_FILE_LOCATION = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + File.separator + "SmartGeometry" + File.separator + "img" + File.separator;


    /**
     * 保存图片
     * @param fileName
     * @return
     */
    public static final boolean saveBitmap(String fileName, Bitmap bitmap){
        createDirInSDCard(IMAGE_FILE_LOCATION);
        File file = createFileInSDCard(IMAGE_FILE_LOCATION + fileName + ".png");
        if(file == null){
            Log.i("hb", " create file error");
            return false;
        }
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false));
            bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
            bos.flush();
            bos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 创建文件
     * @param filePaths
     * @return
     */
    public static File createFileInSDCard(String filePaths){
        File file = new File(filePaths);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }


    /**
     * 创建文件夹
     * @param dirPath
     * @return
     */
    public static File createDirInSDCard(String dirPath){
        File file = new File(dirPath);
        file.mkdirs();
        return file;
    }

    /**
     * 得到所有SG图片文件
     * @return
     */
    public static List<Bitmap> getBitmaps(){
        List<Bitmap> bitmaps = new ArrayList<Bitmap>();

        String[] fileNames = getFileNamesInDIr(IMAGE_FILE_LOCATION);

        for(int i = 0; i<fileNames.length; i++){
            File file = getFileInSDCard(IMAGE_FILE_LOCATION + fileNames[i]);
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
                bitmaps.add(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
        return bitmaps;
    }


    /**
     * 得到文件
     * @param filePath
     * @return
     */
    public static File getFileInSDCard(String filePath){
        File file = new File(filePath);

        return file;
    }

    /**
     * 得到指定文件夹下的所有文件名
     * @param dirPath
     * @return
     */
    public static String[] getFileNamesInDIr(String dirPath){
        File file = new File(dirPath);

        return file.list();
    }
}
